package com.empire.module.member.service;

import com.alibaba.fastjson.JSONArray;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class GenerateLocationData {
    // 杭州城西银泰城中心坐标
    private static final double CENTER_LNG = 120.107171;
    private static final double CENTER_LAT = 30.299418;

    // 最大偏移量（约1公里范围）
    private static final double MAX_OFFSET = 0.01; // 经度/纬度偏移量

    // 生成记录数量
    private static final int RECORD_COUNT = 500;

    // 起始人员ID
    private static final int START_PERSON_ID = 290;

    // 地址列表（城西银泰城周边）
    private static final String[] ADDRESSES = {
            "杭州城西银泰城",
            "杭州城西银泰城购物中心",
            "杭州城西银泰城写字楼A座",
            "杭州城西银泰城写字楼B座",
            "杭州城西银泰城公寓",
            "丰潭路与萍水街交叉口",
            "银泰城周边停车场",
            "萍水街地铁站附近",
            "丰潭路地铁站附近",
            "城西银泰城商圈"
    };

    public static void main(String[] args) {
        List<LocationData> dataList = generateLocationData();
        System.out.println(JSONArray.toJSONString(dataList));
    }

    private static List<LocationData> generateLocationData() {
        List<LocationData> dataList = new ArrayList<>(RECORD_COUNT);
        Random random = new Random();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 生成时间范围：2025-09-03 10:00:00 至 2025-09-03 18:00:00
        long startTime = 1725333600000L; // 2025-09-03 10:00:00
        long endTime = 1725362400000L;   // 2025-09-03 18:00:00

        for (int i = 0; i < RECORD_COUNT; i++) {
            // 生成随机偏移量（确保在中心附近）
            double offsetLng = (random.nextDouble() * 2 - 1) * MAX_OFFSET;
            double offsetLat = (random.nextDouble() * 2 - 1) * MAX_OFFSET;

            // 计算经纬度并保留6位小数
            double lng = round(CENTER_LNG + offsetLng, 6);
            double lat = round(CENTER_LAT + offsetLat, 6);

            // 随机生成时间
            long randomTime = startTime + (long)(random.nextDouble() * (endTime - startTime));
            String locateTime = sdf.format(new Date(randomTime));

            // 随机选择地址
            String address = ADDRESSES[random.nextInt(ADDRESSES.length)];

            // 生成人员ID
            int personId = START_PERSON_ID + random.nextInt(50); // 290-339之间的人员ID

            // 添加到列表
            LocationData data = new LocationData();
            data.setPersonId(personId);
            data.setLocateTime(locateTime);
            data.setLng(lng);
            data.setLat(lat);
            data.setAddress(address);

            dataList.add(data);
        }

        return dataList;
    }

    // 保留指定小数位数
    private static double round(double value, int places) {
        if (places < 0) throw new IllegalArgumentException();

        BigDecimal bd = BigDecimal.valueOf(value);
        bd = bd.setScale(places, RoundingMode.HALF_UP);
        return bd.doubleValue();
    }

    // 定位数据实体类
    public static class LocationData {
        private int personId;
        private String locateTime;
        private double lng;
        private double lat;
        private String address;

        // getter和setter
        public int getPersonId() {
            return personId;
        }

        public void setPersonId(int personId) {
            this.personId = personId;
        }

        public String getLocateTime() {
            return locateTime;
        }

        public void setLocateTime(String locateTime) {
            this.locateTime = locateTime;
        }

        public double getLng() {
            return lng;
        }

        public void setLng(double lng) {
            this.lng = lng;
        }

        public double getLat() {
            return lat;
        }

        public void setLat(double lat) {
            this.lat = lat;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }
    }
}

